package com.example.RuanJianKeShe.Service.login;

import com.alibaba.fastjson.JSONObject;
import com.example.RuanJianKeShe.Bean.Student;
import com.example.RuanJianKeShe.Dao.impl.StudentDaoImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.HashMap;

@WebServlet("/getSnoByLogin")
public class getSnoByLogin extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setContentType("application/json;charset=utf-8");
        HttpSession session = req.getSession();
        Object sno = session.getAttribute("mySno");
        StudentDaoImpl studentDao = new StudentDaoImpl();
        Student student = studentDao.getStudentById(Integer.parseInt(sno.toString()));
        HashMap<String,Object> map = new HashMap<>();
        map.put("name",student.getSname());
        map.put("sno",student.getSno());
        String toJSONString = JSONObject.toJSONString(map);
        resp.getWriter().write(toJSONString);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req,resp);
    }
}
